package com.yellowpage.bigdata.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;
import com.yellowpage.bigdata.action.NspFatallogImeiWeekregionSumAction;
import com.yellowpage.bigdata.action.NspFatallogNumDayregionSumAction;
import com.yellowpage.bigdata.data.NspFatallogNumDayregionSum;
import com.yellowpage.bigdata.dao.NspFatallogNumDayregionSumDao;

public class NspFatallogNumDayregionSumServiceImpl extends BaseServiceImpl
		implements NspFatallogNumDayregionSumService {
	private NspFatallogNumDayregionSumDao nspFatallogNumDayregionSumDao;

	public void setNspFatallogNumDayregionSumDao(
			NspFatallogNumDayregionSumDao nspFatallogNumDayregionSumDao) {
		this.nspFatallogNumDayregionSumDao = nspFatallogNumDayregionSumDao;
	}

	/* Smooth generate Method */
	public NspFatallogNumDayregionSum getNspFatallogNumDayregionSum(Long id) {
		return nspFatallogNumDayregionSumDao.get(id);
	}

	public List<NspFatallogNumDayregionSum> getAllNspFatallogNumDayregionSum() {
		return nspFatallogNumDayregionSumDao.getAll();
	}

	public List<Map> queryNspFatallogNumDayregionSumByPage(Map queryMap,
			PageRender pageRender) {

		String t1 = queryMap.get("requestType").equals("") ? " and REQUEST_TYPE = 'A' " : " and REQUEST_TYPE in "+queryMap.get("requestType");
		String t2 = queryMap.get("regionCode").equals("") ? " and REGION_CODE = 'A' " : " and REGION_CODE in "+queryMap.get("regionCode");
		String sql1="";
		if(NspFatallogNumDayregionSumAction.isType){
			String t3 = " and d.code in "+queryMap.get("requestType");
			sql1="SELECT a.REQUEST_TYPE, a.REGION_CODE , a.LOG_DATE , a.NUM_DAY_COUNT , a.NUM_ALL_COUNT , a.NUMN_DAY_COUNT ,a.NUMN_ALL_COUNT , "
					+"c.COMPANY REQUEST_NAME,d.name CHANNEL,d.code,"
//					+ " ( SELECT c.COMPANY  FROM NSP_REQUEST_TYPE c ,NSP_CHANNEL d where c.code=a.REQUEST_TYPE AND c.CHANNEL_CODE=d.code ) as REQUEST_NAME, "
//					+ " ( SELECT d.name  FROM NSP_CHANNEL d ,NSP_REQUEST_TYPE c where c.CHANNEL_CODE = d.code AND a.REQUEST_TYPE =c.code ) as CHANNEL, "
					+ "(select c.name from OMC_REGION c where c.REGION_CODE=a.REGION_CODE) as REGION_NAME "
					+ "  FROM NSP_FATALLOG_NUM_DAYREGION_SUM a, NSP_CHANNEL d ,NSP_REQUEST_TYPE c WHERE 1=1 AND c.CHANNEL_CODE = d.code AND a.REQUEST_TYPE =c.code" + t3 + t2;
		}else{
			sql1="SELECT a.REQUEST_TYPE, a.REGION_CODE , a.LOG_DATE , a.NUM_DAY_COUNT , a.NUM_ALL_COUNT , a.NUMN_DAY_COUNT ,a.NUMN_ALL_COUNT , "
					+ " ( SELECT c.COMPANY  FROM NSP_REQUEST_TYPE c ,NSP_CHANNEL d where c.code=a.REQUEST_TYPE AND c.CHANNEL_CODE=d.code ) as REQUEST_NAME, "
					+ " ( SELECT d.name  FROM NSP_REQUEST_TYPE c ,NSP_CHANNEL d where c.code=a.REQUEST_TYPE AND c.CHANNEL_CODE=d.code ) as CHANNEL, "
					+ "(select c.name from OMC_REGION c where c.REGION_CODE=a.REGION_CODE) as REGION_NAME "
					+ "  FROM NSP_FATALLOG_NUM_DAYREGION_SUM a WHERE 1=1 " + t1 + t2;
		}
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(sql1)
				.addCondition(" and  LOG_DATE >= :lodDate ")
				.addCondition(" and  LOG_DATE <= :logDate_end ");

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	public void saveNspFatallogNumDayregionSum(
			NspFatallogNumDayregionSum nspFatallogNumDayregionSum) {
		nspFatallogNumDayregionSumDao.save(nspFatallogNumDayregionSum);
	}

	public void updateNspFatallogNumDayregionSum(
			NspFatallogNumDayregionSum nspFatallogNumDayregionSum) {
		nspFatallogNumDayregionSumDao.update(nspFatallogNumDayregionSum);
	}

	public void delete(List deleteList) {
		nspFatallogNumDayregionSumDao.remove(deleteList);
	}

	@Override
	public Map<String, String> getRegionMap(String regionSql) {
		Map<String, String> dataSourceMap = new LinkedMap();
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" select a.REGION_CODE as NAME, a.SHORT_NAME as VALUE from OMC_REGION a where 1 = 1 "+ regionSql);
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
			dataSourceMap.put(resultList.get(i).get("NAME").toString(),
					resultList.get(i).get("VALUE").toString());
		}
		return dataSourceMap;
	}

	@Override
	public Map<String, String> getChannelMap() {
		Map<String, String> dataSourceMap = new LinkedMap();
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" SELECT a.code as NAME, a.name as VALUE  FROM NSP_CHANNEL a  ");
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
			dataSourceMap.put(resultList.get(i).get("NAME").toString(),
					resultList.get(i).get("VALUE").toString());
		}
		return dataSourceMap;
	}

	@Override
	public Map<String, String> getrequestTypeMap() {
		Map<String, String> dataSourceMap = new LinkedMap();
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" SELECT a.COMPANY as NAME, a.CODE as VALUE  FROM NSP_REQUEST_TYPE a  ");
		sqlAssistant.setTail("order by (a.CODE+0)");
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
			dataSourceMap.put(resultList.get(i).get("VALUE").toString(),
					resultList.get(i).get("VALUE") + "-"
							+ resultList.get(i).get("NAME"));
		}
		return dataSourceMap;
	}

	@Override
	public String getrequestTypeString(String query_requestType) {
		String val = "";
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" select c.`CODE` as NAME from NSP_REQUEST_TYPE c where c.channel_code in ("
						+ query_requestType + ") ");
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
			val += "'" + resultList.get(i).get("NAME").toString() + "',";
		}
		return val.substring(0, val.length() - 1);
	}

}